#!/usr/bin/env bash

source /usr/share/kwort/common/default

function show_default() {
	if [ -n "${2}" -a -e "/etc/sysconfig/net/devices/${2}" ]; then
		source /etc/sysconfig/net/devices/$2
		if [ -n "${!1}" ]; then
			echo "<default>${!1}</default>"
		fi
	fi
}

function show_default_pppoe() {
	if [ "${1}" == "PPPOE_USERNAME" ]; then
		USR=$(cat /etc/ppp/pap-secrets | grep -v ^# | awk -F "\"" '{print $2}' | head -n 1)
		if [ -n "${USR}" ]; then
			echo "<default>${USR}</default>"
		fi
	elif [ "${1}" == "PPPOE_PASSWD" ]; then
		PASSWD=$(cat /etc/ppp/pap-secrets | grep -v ^# | awk -F "\"" '{print $4}' | head -n 1)
		if [ -n "${PASSWD}" ]; then
			echo "<default>${PASSWD}</default>"
		fi
	fi	
}

function list_connection_types() {
	if [ -e "/etc/sysconfig/net/devices/${1}" ]; then
		if [ "$(grep "CONNECTION_TYPE=\"dhcp\"" /etc/sysconfig/net/devices/${1})" != "" ]; then
			echo "<item>Obtain IP automatically (DHCP)</item>"
			echo "<item>Static Address</item>"
			echo "<item>ADSL (pppoe)</item>"
			echo "<item>None</item>"
			return 0
		fi
		if [ "$(grep "CONNECTION_TYPE=\"static\"" /etc/sysconfig/net/devices/${1})" != "" ]; then
			echo "<item>Static Address</item>"
			echo "<item>Obtain IP automatically (DHCP)</item>"
			echo "<item>ADSL (pppoe)</item>"
			echo "<item>None</item>"
			return 0
		fi
		if [ "$(grep "CONNECTION_TYPE=\"pppoe\"" /etc/sysconfig/net/devices/${1})" != "" ]; then
			echo "<item>ADSL (pppoe)</item>"
			echo "<item>Obtain IP automatically (DHCP)</item>"
			echo "<item>Static Address</item>"
			echo "<item>None</item>"
			return 0
		fi
		if [ "$(grep "CONNECTION_TYPE=\"none\"" /etc/sysconfig/net/devices/${1})" != "" ]; then
			echo "<item>None</item>"
			echo "<item>ADSL (pppoe)</item>"
			echo "<item>Obtain IP automatically (DHCP)</item>"
			echo "<item>Static Address</item>"
			return 0
		fi
	fi
	# Fall back
	echo "<item>Obtain IP automatically (DHCP)</item>"
	echo "<item>Static Address</item>"
	echo "<item>ADSL (pppoe)</item>"
	echo "<item>None</item>"
}

export MAIN_DIALOG="<window title=\"Kwort Network Manager\" icon-name=\"${KNM_IMG}\" window_position=\"1\" resizable=\"false\">
<vbox>
	<notebook labels=\"Basic configuration|PPPoE\">
		<frame Ethernet Configuration Settings: Device - ${1}>
			<hbox>
				<text>
					<label>Connection Type: </label>
				</text>
				<combobox case-sensitive=\"true\" value-in-list=\"true\" allow-empty=\"false\">
					<variable>CONNECTION_TYPE</variable>
					$(list_connection_types ${1})
				</combobox>
			</hbox>
			<hbox>
				<text>
					<label>IP Address: </label>
				</text>
				<entry>
					$(show_default IP ${1})
					<variable>IP</variable>
				</entry>
			</hbox>
			<hbox>
				<text>
					<label>Network Mask: </label>
				</text>
				<entry>
					$(show_default MASK ${1})
					<variable>MASK</variable>
				</entry>
			</hbox>
			<hbox>
				<text>
					<label>Broadcast Address: </label>
				</text>
				<entry>
					$(show_default BROADCAST ${1})
					<variable>BROADCAST</variable>
				</entry>
			</hbox>
		</frame>
		<frame>
			<hbox>
				<text>
					<label>Username: </label>
				</text>
				<entry>
					$(show_default_pppoe PPPOE_USERNAME)
					<variable>PPPOE_USERNAME</variable>
				</entry>
			</hbox>
			<hbox>
				<text>
					<label>Password: </label>
				</text>
				<entry invisible_char=\"*\" visibility=\"false\">
					$(show_default_pppoe PPPOE_PASSWD)
					<variable>PPPOE_PASSWD</variable>
				</entry>
			</hbox>
		</frame>
	</notebook>
	<hbox>
		<button cancel>
			<input file>${CANCEL_IMG}</input>
		</button>
		<button ok>
			<input file>${OK_IMG}</input>
		</button>
	</hbox>
</vbox>
</window>"

if [ -z "${1}" ]; then
	exit
fi

eval $(gtkdialog --program=MAIN_DIALOG)

if [ "${EXIT}" == "Cancel" -o "${EXIT}" == "abort" ]; then
	exit
fi

echo "DEV_TYPE=\"ethernet\"" > /etc/sysconfig/net/devices/${1}
case "${CONNECTION_TYPE}" in
	'Obtain IP automatically (DHCP)')
		echo "CONNECTION_TYPE=\"dhcp\"" >> /etc/sysconfig/net/devices/${1}
	;;
	'Static Address')
		echo "CONNECTION_TYPE=\"static\"" >> /etc/sysconfig/net/devices/${1}
		if [ -z "${MASK}" ]; then
			MASK="255.255.255.0"
		fi
		if [ -z "${BROADCAST}" ]; then
			BIFS="$IFS"
			IFS='.'
			A=($IP)
			BROADCAST="${A[0]}.${A[1]}.${A[2]}.255"
			IFS="$BIFS"
		fi
	;;
	'ADSL (pppoe)')
		echo "CONNECTION_TYPE=\"pppoe\"" >> /etc/sysconfig/net/devices/${1}
	;;
	'None')
		echo "CONNECTION_TYPE=\"none\"" >> /etc/sysconfig/net/devices/${1}
	;;
esac
echo "IP=\"${IP}\"" >> /etc/sysconfig/net/devices/${1}
echo "MASK=\"${MASK}\"" >> /etc/sysconfig/net/devices/${1}
echo "BROADCAST=\"${BROADCAST}\"" >> /etc/sysconfig/net/devices/${1}

if [ "${CONNECTION_TYPE}" == 'ADSL (pppoe)' ]; then
	echo -e "\"${PPPOE_USERNAME}\"\t*\t\"${PPPOE_PASSWD}\"" > /etc/ppp/pap-secrets
	echo -e "\"${PPPOE_USERNAME}\"\t*\t\"${PPPOE_PASSWD}\"" > /etc/ppp/chap-secrets
	cat << EOF > /etc/ppp/pppoe.conf
ETH='${1}'
USER='${PPPOE_USERNAME}'
DEMAND=no
PEERDNS=yes
DNS1=
DNS2=
DEFAULTROUTE=yes
CONNECT_TIMEOUT=30
CONNECT_POLL=2
ACNAME=
SERVICENAME=
PING="."
PIDFILE="/var/run/\$CF_BASE-pppoe.pid"
SYNCHRONOUS=no
CLAMPMSS=1412
LCP_INTERVAL=20
LCP_FAILURE=3
PPPOE_TIMEOUT=80
FIREWALL=NONE
LINUX_PLUGIN=
PPPOE_EXTRA=""
PPPD_EXTRA=""
EOF
fi
if [ "${CONNECTION_TYPE}" == 'ADSL (pppoe)' -o "${CONNECTION_TYPE}" == 'Obtain IP automatically (DHCP)' ]; then
	echo "GATEWAY=\"\"" > /etc/sysconfig/net/gateway
fi

/etc/rc.d/rc.net restart ${1}

#EOF
